home *** CD-ROM | disk | FTP | other *** search
- /*
- * $Log: arcpack.c,v $
- * Revision 1.3 88/04/11 18:57:00 hyc
- * fixed a typo...
- *
- * Revision 1.2 88/04/11 18:38:15 hyc
- * added support for squashing, re-synch with MTS
- *
- * Revision 1.1 88/04/11 18:27:10 hyc
- * Initial revision
- *
- * Revision 1.1 87/12/19 04:05:16 hyc
- * Initial revision
- *
- * Revision 1.3 87/08/13 17:05:11 hyc
- * Run thru indent, fixed some signed vs. unsigned problems
- * with bp <-> buf, and inbuf and localbuf...
- * Revision 1.2 87/07/21 08:57:19 hyc *** empty
- * log message ***
- *
- */
-
- /*
- * ARC - Archive utility - ARCPACK
- *
- * Version 3.46, created on 10/23/86 at 17:47:06
- *
- * (C) COPYRIGHT 1985,86 by System Enhancement Associates; ALL RIGHTS RESERVED
- *
- * By: Thom Henderson
- *
- * Description: This file contains the routines used to compress a file when
- * placing it in an archive.
- *
- * Language: Computer Innovations Optimizing C86
- */
- #include <stdio.h>
- #include "arc.h"
- #ifdef MTS
- #include <ctype.h>
- #endif
-
- /* stuff for non-repeat packing */
-
- #define DLE 0x90 /* repeat sequence marker */
-
- static unsigned char state; /* current packing state */
-
- /* non-repeat packing states */
-
- #define NOHIST 0 /* don't consider previous input */
- #define SENTCHAR 1 /* lastchar set, no lookahead yet */
- #define SENDNEWC 2 /* run over, send new char next */
- #define SENDCNT 3 /* newchar set, send count next */
-
- /* packing results */
-
- static LONG stdlen; /* length for standard packing */
- static INT crcval; /* CRC check value */
-
- INT
- pack(f, t, hdr) /* pack file into an archive */
- FILE *f, *t; /* source, destination */
- struct heads *hdr; /* pointer to header data */
- {
- INT c; /* one character of stream */
- LONG ncrlen; /* length after packing */
- LONG lzwlen; /* length after crunching */
- LONG pred_cm(), sqpred_cm(); /* dynamic crunching cleanup */
- LONG tloc, ftell(); /* start of output */
- INT getch();
- INT getc_ncr();
- INT putc_pak();
-
- /* first pass - see which method is best */
-
- tloc = ftell(t); /* note start of output */
-
- if (!nocomp) { /* if storage kludge not active */
- if (note) {
- printf(" analyzing, ");
- fflush(stdout);
- }
- state = NOHIST; /* initialize ncr packing */
- stdlen = ncrlen = 0; /* reset size counters */
- crcval = 0; /* initialize CRC check value */
- setcode(); /* initialize encryption */
-
- if (dosquash) {
- sqinit_cm(f, t);
- while ((c = getch(f)) != EOF) { /* for each byte of file */
- ncrlen++; /* one more packed byte */
- sqputc_cm(c, t); /* see what squashing
- * can do */
- }
- lzwlen = sqpred_cm(t);
- } else {
- init_cm(f, t); /* initialize for crunching */
-
- while ((c = getc_ncr(f)) != EOF) { /* for each byte of file */
- ncrlen++; /* one more packed byte */
- putc_cm(c, t); /* see what crunching can do */
- }
- lzwlen = pred_cm(t); /* finish up after crunching */
- }
- } else { /* else kludge the method */
- stdlen = 0; /* make standard look best */
- ncrlen = lzwlen = 1;
- }
-
- /* standard set-ups common to all methods */
-
- fseek(f, 0L, 0); /* rewind input */
- hdr->crc = crcval; /* note CRC check value */
- hdr->length = stdlen; /* set actual file length */
- state = NOHIST; /* reinitialize ncr packing */
- setcode(); /* reinitialize encryption */
-
- /* choose and use the shortest method */
-
- if (kludge && note)
- printf("\n\tS:%ld P:%ld C:%ld,\t ", stdlen, ncrlen, lzwlen);
-
- if (stdlen <= ncrlen && stdlen <= lzwlen) {
- if (note) {
- printf("storing, "); /* store without compression */
- fflush(stdout);
- }
- hdrver = 2; /* note packing method */
- fseek(t, tloc, 0); /* reset output for new method */
- if (nocomp) { /* store only kludge skips things */
- stdlen = crcval = 0; /* recalc these for kludge */
- while ((c = getch(f)) != EOF) /* store it straight */
- putc_pak(c, t);
- } else
- filecopy(f, t, stdlen); /* else use fast file copier */
- hdr->crc = crcval;
- hdr->length = hdr->size = stdlen;
- } else if (ncrlen < lzwlen) {
- if (note)
- printf("packing, "); /* pack with repeat
- * suppression */
- hdrver = 3; /* note packing method */
- hdr->size = ncrlen; /* set data length */
- fseek(t, tloc, 0); /* reset output for new method */
- while ((c = getc_ncr(f)) != EOF)
- putc_pak(c, t);
- } else {
- if (note)
- printf(dosquash ? "squashed, " : "crunched, ");
- hdrver = dosquash ? 9 : 8;
- hdr->size = lzwlen; /* size should not change */
- }
-
- /* standard cleanups common to all methods */
-
- if (note)
- printf("done.\n");
- }
-
- /*
- * Non-repeat compression - text is passed through normally, except that a
- * run of more than two is encoded as:
- *
- * <char> <DLE> <count>
- *
- * Special case: a count of zero indicates that the DLE is really a DLE, not a
- * repeat marker.
- */
-
- INT
- getc_ncr(f) /* get bytes with collapsed runs */
- FILE *f; /* file to get from */
- {
- static INT lastc; /* value returned on last call */
- static INT repcnt; /* repetition counter */
- static INT c; /* latest value seen */
-
- switch (state) { /* depends on our state */
- case NOHIST: /* no relevant history */
- state = SENTCHAR;
- return lastc = getch(f); /* remember the value next
- * time */
-
- case SENTCHAR: /* char was sent. look ahead */
- switch (lastc) {/* action depends on char */
- case DLE: /* if we sent a real DLE */
- state = NOHIST; /* then start over again */
- return 0; /* but note that the DLE was real */
-
- case EOF: /* EOF is always a special case */
- return EOF;
-
- default: /* else test for a repeat */
- for (repcnt = 1; (c = getch(f)) == lastc && repcnt < 255; repcnt++);
- /* find end of run */
-
- switch (repcnt) { /* action depends on run size */
- case 1:/* not a repeat */
- return lastc = c; /* but remember value
- * next time */
-
- case 2:/* a repeat, but too short */
- state = SENDNEWC; /* send the second one
- * next time */
- return lastc;
-
- default: /* a run - compress it */
- state = SENDCNT; /* send repeat count
- * next time */
- return DLE; /* send repeat marker this
- * time */
- }
- }
-
- case SENDNEWC: /* send second char of short run */
- state = SENTCHAR;
- return lastc = c;
-
- case SENDCNT: /* sent DLE, now send count */
- state = SENDNEWC;
- return repcnt;
-
- default:
- arc_abort("Bug - bad ncr state\n");
- }
- }
-
- INT
- getch(f) /* special get char for packing */
- FILE *f; /* file to get from */
- {
- INT c; /* a char from the file */
- #ifndef MSDOS
- static INT cr = 0; /* add \r before \n ? */
-
- if (cr) {
- c = '\n';
- #ifdef MTS
- c = toascii(c);
- #endif
- crcval = addcrc(crcval, c);
- stdlen++;
- cr = 0;
- return (c);
- }
- #endif
-
- if ((c = fgetc(f)) != EOF) { /* if not the end of file */
- #ifndef MSDOS
- if (!image && (c == '\n')) {
- c = '\r';
- cr = 1;
- }
- #endif
- #ifdef MTS
- if (!image)
- c = toascii(c);
- #endif
- crcval = addcrc(crcval, c); /* then update CRC check
- * value */
- stdlen++; /* and bump length counter */
- }
- return c;
- }
-
- INT
- putc_pak(c, f) /* put a packed byte into archive */
- char c; /* byte to put */
- FILE *f; /* archive to put it in */
- {
- putc_tst(code(c), f); /* put encoded byte, with checks */
- }
-